package com.oa.tools.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.landray.kmss.sys.organization.webservice.in.SysSynchroSetOrgContext;

public class ObjectUtil {

	private static final ObjectMapper mapper = new ObjectMapper();
	private static final XmlMapper xmlMapper = new XmlMapper();

	public static Set<String> getBeanFieldSet(Class<?> beanClass, String ignore) {
		Field[] fields = beanClass.getDeclaredFields();
		Set<String> set = new HashSet<>();
		for (Field field : fields)
			if ((ignore == null) || (!field.getName().matches(ignore))) {
				int mod = field.getModifiers();
				if ((!Modifier.isStatic(mod)) && (!Modifier.isFinal(mod))) {
					set.add(field.getName());
				}
			}
		return set;
	}

	public static <T> T map2Object1(Map<String, Object> map, Class<T> cls) throws Exception {
		if (map == null) {
			return null;
		}
		T t = cls.newInstance();

		Field[] fields = t.getClass().getDeclaredFields();
		for (Field field : fields) {
			int mod = field.getModifiers();
			
			if ((!Modifier.isStatic(mod)) && (!Modifier.isFinal(mod))) {
				field.setAccessible(true);
				field.set(t, map.get(field.getName()));
			}
		}
		return t;
	}
	
	public static <T> T map2Object(Map<String, Object> map, Class<T> cls) throws Exception {
		if (map == null) {
			return null;
		}
		T t = cls.newInstance();

		Field[] fields = t.getClass().getDeclaredFields();
		for (Field field : fields) {
			int mod = field.getModifiers();
			Object val =  map.get(field.getName());
			Method m = cls.getDeclaredMethod("set"+StringUtil.toUpperCaseFirstOne(field.getName()),val.getClass());
			m.invoke(t,val);
		}
		return t;
	}
	
	public static Map<String, Object> object2Map(Object obj) throws Exception {
		if (obj == null) {
			return null;
		}

		Map<String, Object> map = new HashMap<>();
		Field[] declaredFields = obj.getClass().getDeclaredFields();
		for (Field field : declaredFields) {
			field.setAccessible(true);
			map.put(field.getName(), field.get(obj));
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> json2Map(String jsonStr) {
		try {
			return json2Object(jsonStr, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> xml2Map(String jsonStr) {
		try {
			return xml2Object(jsonStr, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String object2Json(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String object2JsonPretty(Object obj) {
		try {
			return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static <T> T json2Object(String jsonStr, Class<T> cls) {
		try {
			return mapper.readValue(jsonStr, cls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static <T> T xml2Object(String xmlStr, Class<T> cls) throws Exception {
		T obj = xmlMapper.readValue(xmlStr, cls);
		return obj;
	}

	public static <T> String object2Xml(T bean) throws Exception {
		String string = xmlMapper.writeValueAsString(bean);
		return string;
	}

	public static String object2XmlPretty(Object obj) {
		try {
			return xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		char c = ' ';
		int i = (int)c;
		// \u4e00-\u9fa5
		String hexStr = Integer.toHexString(i);
		System.out.println(hexStr);
		System.out.println("\u4dae\u00a0");
		String name = "黄䶮康";
		System.out.println(name);
//		name = name.replaceAll("[^\u4e00-\u9fa5\\-a-zA-Z0-9]", "");
		
		name = name.replaceAll("[x00-xff]", "");
		
		System.out.println(name);
		
	}
}